/**
 * 诊断和办理入院
 */

/**
 * 加载诊断信息下拉数据
 */
var zhenduanindex=1;//诊断内容ID递增序数
$(document).ready(function() {
	loadSelectAllList();
	handleAdmission();
});
function loadSelectAllList()
{
	/*$.ajaxSetup({  
	    async : false  
	});*/
	//诊断依据
	var url2 = rootUrl + "/cliparamter/yijuList";
	var params2 = {};
	$.get(url2, params2, function(data2) {
		
		var list2 = data2.list;
		zhenduanyijuHtml = "<select>";
		zhenduanyijuHtml += "<option value='-1'>-请选择-</option>";
		for (var i = 0; i < list2.length; i++) {
			var option ="<option value="+list2[i]["bgp01"]+">"+list2[i]["bgp02"]+"</option>";
			zhenduanyijuHtml += option;
		}
		zhenduanyijuHtml+="</select>";
		//诊断结果
		var url3 = rootUrl + "/cliparamter/zhenduanjieguoList";
		var params3 = {};
		$.get(url3, params3, function(data3) {
			var list3 = data3.list;
			zhenduanyieguoHtml ="<select>";
			zhenduanyieguoHtml += "<option value='-1'>-请选择-</option>";
			for (var j = 0; j < list3.length; j++) {
				var option ="<option value="+list3[j]["ady01"]+">"+list3[j]["ady02"]+"</option>";
				zhenduanyieguoHtml += option;
			}
			zhenduanyieguoHtml+="</select>";
			shouyeEvent();//首页
		});
	});
}
//首页
function shouyeEvent(){
	$('#shouye_btn').on("click",function(){
		if (vaa01) {
			debugger;
			params = {};
			params.vac01 = vac01;
			//获取病人诊断的信息
    			$.ajax({
    				data:params,
    				url : rootUrl + "/treatment/getZhenduanShouye",
    				type:'get',
    				dataType:'json',
    				success:function(data){
				    		laytplUrl(myurl+"/module/shouye_table.html",data.list,function(data,html){
				    			var index = layer.open({
				    				type : 1,
				    				scrollbar : false,
				    				title : "诊断信息",
				    				area : [ "80%", "80%"],
				    				shade : 0.3,
				    				//maxmin : true,
				    				content : html,
				    				closeBtn:1,
				    				success : function() {
				    					$("select").select2({
				    						language: "zh-CN"
				    					});

				    					deleteTr();
				    					addzhenduanSelect();
				    					$("#remove_action_zd").on("click",function() {
											layer.close(index);
										})
				    				}
				    			})
				    		});
    	}
    });
}	else{
    		layer.msg("请选择病人", {time : 2000, icon : 0,skin: 'layui-layer-msg0'});
    		$('#new_order_list a[href="#Jiezhen"]').tab('show');
    	}
	})
}
//编辑数据增加下拉框
function addzhenduanSelect(){
	$("#zhenduan_tbody tr").each(function(i,e){
//		if(($(e).find(".shouye_yzd").text())=='0'){
			var id =$(e).find(".shouye_zdms").attr("id");
			zhenduaninputEvent(id);
			
//		}
	})
}
//增加行
function zhenduanCloneTr(){
laytplUrl(myurl+"/module/shouye_tr_clone.html",{},function(data,html){
	$("#zhenduan_tbody").append(html);
	zhenduanindex++;
	$("select").select2({});
	var id=$(".shouye_zdms").last().attr("id");
	zhenduaninputEvent(id);
	deleteTr();
});
}
//删除行
function deleteTr(){
$(".shouye_cz").on("click",function(){
	$(this).parents("tr").remove();
})
}
//首页诊断下拉
//input输入事件事件
function zhenduaninputEvent(id){
	var currentInput=$("#"+id).focus();
	/*按回车按钮后弹出下拉框*/
	currentInput.keydown(function (e) {
		var inputText=$(this).val();
      if (e.which === 13) {
      	var that=this;
      	zhenduangetDropList(id);
      	
      }
      });
	
	$("#"+id).on("click",function(){//点击出现下拉
		var inputText=$(this).val();
		zhenduangetDropList(id);
	})
}

/* 点击出现下拉框 */
function zhenduangetDropList(inputId){
			shouyeActiveId=inputId;
	    	var thisLeft=$("#"+inputId).position().left;
			var thisTop=$("#"+inputId).position().top;
			var thisheight=$("#"+inputId).parent().outerHeight()-7;
			$(".selectDiv").slideDown();
			$(".selectDiv").css({
				position:"absolute",
				left:thisLeft,
				top:thisTop+thisheight+"px",
				display:"block"
			})
			$("#refund").css({
				"z-index":999
			})
	var shouyeobj={
		 "tree":"zhenduan-tree",//第一个tab（常用诊断）的左边tree id
			 "search_jibing":"search_jibing",
			 "chengyongzhenduan":"shouye_xydqzd",
			 "tree_table":"zhenduan-tree-table"
		}
		banliruyuan_zhenduan(shouyeobj);
	
} 
function add_zhenduan(zdid, zdname){
   $("#"+shouyeActiveId).val(zdname);
   $("#"+shouyeActiveId).attr("data-info",zdid);
} 
//保存诊断信息
function saveZhenduan(){
		var vao1 = [];
		//1过滤空行
		$("#zhenduan_tbody tr").each(function(i,ele){
			if(!($(ele).find(".shouye_zdms").val())){
				$(ele).find(".shouye_cz").trigger("click");
			}
			else
			{
				var obj={};
				obj.vao07=$(ele).find(".shouye_zdsx").val();//诊断顺序
				obj.vao15=$(ele).find(".shouye_zdms").val();//诊断内容
				obj.cam01=$(ele).find(".shouye_zdms").attr("data-info");//诊断内容id
				obj.bgp01=$(ele).find(".shouye_zdyj").find("select option:selected").val();//诊断依据
				obj.ady01=$(ele).find(".shouye_zdjg").find("select option:selected").val();//诊断结果
				if($(ele).find(".shouye_yz").is(':checked')){//疑诊
					obj.vao18=1;
				}else{
					obj.vao18=0;
				}
				obj.vaf01 = $(ele).find(".shouye_yzd").text();//医嘱单
				obj.vac01 = vac01;
				obj.vaa01 = vaa01;
				vao1.push(obj);
			}
		})
		//2数据合法性校验
		
		//3 数据持久化
		$.ajax({
			data:{"vao1":JSON.stringify(vao1),"vac01":vac01},
			url : rootUrl + "/treatment/saveZhenduanShouye",
			type:'post',
			dataType:'json',
			success:function(data){
				if(data){
					if(data.result=='Success'){
						layer.msg("诊断信息保存成功！",{time:2000,icon:1,skin: 'layui-layer-msg1'});
					}
				}else{
					layer.msg("数据异常，请联系管理员！",{time:2000,icon:2,skin: 'layui-layer-msg1'});
				}
			}	
		});
		
	
}

/*********************************************************banliruyuan**************************************************************/

//办理入院
function handleAdmission() {
	var that = this;
	$("#handle_admission").on("click",function() {
				// var vaa01=$("#vaa01").val();
				if (vaa01) {
					var url = rootUrl + "/treatment/handleAdmission";
					var params = {};
					$.get(url, params, function(data) {
						that.laytplUrl(myurl + "/module/banliruyuan.html", data,function(data, html) {
									var index = layer.open({
										type : 1,
										// scrollbar : false,
										title : "办理入院",
										area : [ "80%", "80%" ],
										shade : 0.1,
										maxmin : true,
										content : html,
										closeBtn : 1,
										success : function() {
											$("select").select2({
												language: "zh-CN"
											});
											xySelectDiv();
											var date = new Date();
											var vbn11 = new Date();
											vbn11.setDate(date.getDate() + 3);
											vbn11 = vbn11.Format("yyyy-MM-dd");
											var vbn10 = date
													.Format("yyyy-MM-dd");
											var vbn04 = date
													.Format("yyyy-MM-dd");
											$("#vbn10").val(vbn10);
											$("#vbn11").val(vbn11);
											$("#vbn04").val(vbn04);
											$("#remove_action").on("click",function() {
												layer.close(index);
											})
										}
									});
								});
					});
				} else {
					
					layer.msg("请选择病人", {time : 2000, icon : 0,skin: 'layui-layer-msg0'});
					$('#new_order_list a[href="#Jiezhen"]').tab('show');
				}

			})
}


/* 诊断下拉框 */
function xySelectDiv() {
	$(".bootstrap-tagsinput").on("click", function() {
		 var e=arguments.callee.caller.arguments[0]||event; 
		 if (e && e.stopPropagation) { 
		  // this code is for Mozilla and Opera
		  e.stopPropagation(); 
		 } else if (window.event) { 
		  // this code is for IE 
		  window.event.cancelBubble = true; 
		 } 
		var thisLeft = $(this).position().left;
		var thisTop = $(this).position().top;
		var thisheight = $(this).outerHeight() + 1;
		$("#banliruyuan_zhenduan").slideDown();
		$("#banliruyuan_zhenduan").css({
			position : "absolute",
			left : thisLeft,
			top : thisTop + thisheight + "px",
			display : "block"
		})
		$("#refund").css({
			"z-index" : 999
		})
		var obj={
			"tree":"bangliruyuan-tree",
			 "search_jibing":"search_jibing",
			 "chengyongzhenduan":"blry_xydqzd",
			 "tree_table":"ruyuan-tree-table"
		}
		banliruyuan_zhenduan(obj);
		
		// hideDiv(".selectDiv");
	})
}
// 办理入院诊断数据加载
function banliruyuan_zhenduan(obj) {
	// 加载疾病
	var url = rootUrl + "/treatment/jibingList";
	var params = {};
	$.get(url, params, function(data) {
		var list = data.list;
		$('#'+obj.tree).treeview({
			data : list
		});//办理入院
		$('#'+obj.tree).on('nodeSelected', function(event, data) {
			$("#"+obj.search_jibing).val('');
			// 查询疾病详细项目列表
			ruyuanXiangxiList(data['id'],obj);
		});
	});
	var data = ruyuanbingrenVaoList;
	if (data) {
		laytplUrl(myurl + "/module/zhenduan_table.html", data, function(
				data, html) {
			$("#"+obj.chengyongzhenduan).html(html);
		});
	}

}
// 入院诊断疾病-根据cag01查询详细
function ruyuanXiangxiList(cag01,obj) {
	var url = rootUrl + "/treatment/jibingInfo";
	var params = {};
	params.cag01 = cag01;
	$.get(url, params, function(data) {
		if (data) {
			laytplUrl(myurl + "/module/tree-table-jibing.html", data,
					function(data, html) {
						$("#"+obj.tree_table).empty().append(html);
						
					});
		}
	});
}


//诊断标签添加 cam01类型 
function zdSelectResult(zdid, zdname) {
	if($("#ruyuan_select").attr("data-role")){
		$("#ruyuan_select").tagsinput('add', zdname, zdid);
	}else{
		add_zhenduan(zdid, zdname);
	}
	$(".selectDiv").hide();
}
//诊断标签添加 bak01类型 即查询
function zdSearchSelectResult(zdid,zdname){
	 var str = '{"bak01":"'+zdid+'"}';
	$("#lczds_select").tagsinput('add', zdname,str);
	$(".selectDiv").hide();
	
}

/*
 * 保存入院信息
 */
function saveRuyuan()
{
	//校验
	//获取数据
	var n = /^[0-9]*[1-9][0-9]*$/;// 正整数；
//	 var flag = $("#isPrintRuyuan").parent().attr("class").indexOf("checked");
	var flag=$("#isPrintRuyuan").is(':checked');
	var ruyuankeshi = $("#ruyuankeshi").find("option:selected").val();
	var vbn09 = $("#vbn09").val();
	if(!n.test(vbn09)){
		layer.alert("请填写正确的预交金金额！",{icon:0})
		return false;
	}
	var vbn10 = $("#vbn10").text();
	var vbn11 = $("#vbn11").text();
	var vbn14 = $("#vbn14").val();
	var vbn04 = $("#vbn04").text();

	//新增或者修改数据
	 var url = rootUrl + "/treatment/saveRuyuan";
	 var params = {};
	 var vbn1 = {};
	 //入院信息
	 vbn1.bce01 = bce01;
	 vbn1.bce03 = bce03;
	 vbn1.bck01b = bck01;
	 vbn1.vac01 = vac01;
	 vbn1.vaa01 = vaa01;
	 vbn1.bck01 = ruyuankeshi;
	 vbn1.vbn09 = vbn09;
	 vbn1.vbn10 = vbn10;
	 vbn1.vbn11 = vbn11;
	 vbn1.vbn14 = vbn14;
	 vbn1.vbn04 = vbn04;
	 //诊断信息
	 var vao1Arr = [];
	 var temp = $(".bootstrap-tagsinput").children("span");
	 for(var i=0;i<temp.length;i++)
	 {
		 var cam01 = $(temp[i]).attr("data-info");
		 var vao15 = $(temp[i]).text();
		 var vao1 = {};
		 vao1.vac01 =vac01;
		 vao1.vaa01 = vaa01;
		 vao1.vao01 = cam01;
    	 vao1.vao07 = "0";
    	 vao1.vaf01 = "0";
    	 vao1.vao11 = "1";
    	 vao1.cam01 = cam01;
    	 vao1.vao15 = vao15;
    	 vao1.bak01a = "0";
    	 vao1.vao18 = "0";
    	 vao1.vao22 = "0";
    	 vao1.fsign = "1";
    	 vao1.bgp01 = "";
    	 vao1.ady01 = "";
    	 vao1Arr.push(vao1);
	 }
	 params.vbn1 = JSON.stringify(vbn1);
	 params.vao1 = JSON.stringify(vao1Arr);
	 $.get(url,params,function(data){
		 if(data)
		 {
			 if(data.result=="Success")
			 {
				 layer.msg("办理入院成功！", {time:2000,icon:1,skin: 'layui-layer-msg1'});
//				 layer.closeAll();
//				 parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭弹出层
				
				 if(flag)
				 {
					 print2(110,$("#vac01").val());
				 }
			 }
			 else 
			 {
				 layer.msg(data.resultMsg, {time:2000,icon:2,skin: 'layui-layer-msg2'});
			 }
		 }
		 else
		 {
			 layer.msg("数据异常，请联系管理员", {time:2000,icon:2,skin: 'layui-layer-msg2'});
		 }
	 });
}






















